<?php
//企业管理
if(!defined('IN_T')){
   die('hacking attempt');
}

class module_admin_member extends module_public_member{

	//重写提取列表
	protected function index(){
		global $tp;
		global $Json;
		global $Db;
		global $Base;
		$spm = '';
		$size = 15;
		$page = intval($_REQUEST['page']);
		$page = $page<1 ? 1 : $page;
		$name = Common::sfilter($_REQUEST['name']);
	    $priv = $this->memberList($name,$page,$size);

	    // var_dump($name);
		$pages = Common::set_page($size,$page,$priv['count']);
		foreach ($pages as $key => $value) {
			$pages[$key]['url'] = "/".COMPANY_PATH."/?m=member&page=".$value['num'].$priv['spm'];
		}

		$tp->assign("page",$page);
		$tp->assign("pages",$pages);
		$tp->assign('res',$priv);
		// var_dump($pages);
	}

	//管理员编辑member
	//绑定company_id的pk_works_main
	protected function detail(){
		global $Db;
		global $Base;
		global $Json;
		global $tp;
		$id = intval($_REQUEST['id']);  //当前操作的company账号id
		//显示界面
		if(empty($_POST)){
			parent::edit();
			//提取绑定的场景
			$company_profile = $Db->query("SELECT * FROM ".$Base->table('company_profile')." WHERE c_id = $id","Row");
			// var_dump($company);
			$worksmain = $this->getWorksmain();
			//展示所有分类
			$classification = $Db->query("SELECT * FROM ".$Base->table('company_type')." ");
			// var_dump($classification);
			$tp->assign('classification',$classification);
			// var_dump($worksmain);
			$tp->assign('worksmain',$worksmain);
			$tp->assign('company_profile',$company_profile);

		}
		//处理数据
		else{
			$id = parent::edit();	//当前操作的company_id
			// var_dump($id);
			if($id){
				$pid = intval($_POST['pid']);	//要绑定的全景项目id
				$type = intval($_POST['type']);
				$comment_name = Common::sfilter($_POST['comment_name']);//留言按钮名称
				$menu_name = Common::sfilter($_POST['menu_name']);//菜单名称
				$menu_status = intval($_POST['menu_status']);//菜单状态
				$pano_url = Common::sfilter($_POST['pano_url']); //全景项目链接
				$res['status'] = 0;
				// var_dump($pid);
				// echo "select pk_works_main from ".$Base->table('worksmain')." where pk_works_main=$pid";die;
				//检查全景id是否存在
				//if(!$Db->query("select pk_works_main from ".$Base->table('worksmain')." where pk_works_main=$pid",'One')){
				//	$res['msg'] = '请为商家绑定一个全景项目';
				//}
				//全景项目
				if(empty($pano_url)){
					$res['msg'] = '请为商家绑定一个全景项目';
				}
				else{
					// var_dump($pid);
					//判断商户基本信息是否存在
					$company_profile_id = $GLOBALS['Db']->query("SELECT id FROM ".$GLOBALS['Base']->table('company_profile')." WHERE c_id = $id","One");
					$data = array(
						'c_id'=>$id,
						'pk_works_main'=>$pid,
						'pano_url' => $pano_url,
						'type'=>$type,
                        'comment_name'=>$comment_name,
                        'menu_name'=>$menu_name,
                        'menu_status'=>$menu_status,
						'like' => intval($_POST['like']),
						'unlike' => intval($_POST['unlike']),
                    );
					// var_dump($data);
					if(empty($company_profile_id)){
						$GLOBALS['Db']->insert($GLOBALS['Base']->table('company_profile'),$data);
					}
					else{
						// var_dump($data);
						$GLOBALS['Db']->update($GLOBALS['Base']->table('company_profile'),$data,array('id'=>$company_profile_id));
					}
					$res = array('status'=>'1','msg'=>'提交成功','href'=>'/'.COMPANY_PATH.'/?m=member');
				}
				echo $Json->encode($res);
				exit;
			}
		}
	}

	//响应前台ajax查询所有全景
	protected function get_projects(){
		global $Json;
		$s_pname = Common::sfilter($_REQUEST['pname']);
		$worksmain =$this->getWorksmain($s_pname);
		echo $Json->encode($worksmain);
		exit;
	}

	//调用父类禁用
	protected function is_not(){

		module_public_member::dismember();
	}

	//重写语言变量
	protected function set_lang(){
		global $tp;
		$tp->assign('nav','企业管理');
	}

	private function memberList($name,$page,$size){
		$spm = '';
		$sql = "from ".$GLOBALS['Base']->table('company')." as c ".
			" left join ".$GLOBALS['Base']->table('company_profile')." as p on c.id=p.c_id".
			" left join ".$GLOBALS['Base']->table('worksmain')." as w on p.pk_works_main=w.pk_works_main".
			" left join ".$GLOBALS['Base']->table('company_type')." as pc on pc.id = p.type ".
			" where  c.p_id=0";

		if($name){
			$sql .= " AND c.name LIKE '%$name%' ";
			$spm = "&name=$name";
			$GLOBALS['tp']->assign('name',$name);
		}
	    $rescount = $GLOBALS['Db']->query("SELECT COUNT(*) AS num " .$sql,"One");
	    $sql .= " ORDER BY c.id DESC  LIMIT ".($page-1)*$size.", ".$size."";
	    $sql = "select c.*,w.thumb_path,pc.name as pc_name ".$sql;
	    $res = $GLOBALS['Db']->query($sql);
	    foreach ($res as $key => $value) {
	    	$sql ="select * from ".$GLOBALS['Base']->table('company')." where p_id=".$value['id']."";
			$res[$key]['admin'] = $GLOBALS['Db']->query($sql);
			$sql ="select name from ".$GLOBALS['Base']->table('company_profile')." where c_id=".$value['id']."";
			$res[$key]['type'] = $GLOBALS['Db']->query($sql,"One");
		}
	    return array('res'=>$res,'count'=>$rescount,'spm'=>$spm);
	}

	private function getWorksmain($s_pname){
		//管理员调用所有全景
		$sql = "SELECT name,pk_works_main FROM ".$GLOBALS['Base']->table('worksmain')."";
		if($s_pname){
			$sql .= " WHERE name LIKE '%$s_pname%' ";
		}
		// echo $sql;
		return $GLOBALS['Db']->query($sql." ORDER BY pk_works_main DESC");
	}

}
new module_admin_member();
?>